import html from '../../assets/partials/tenant-status.html';

let interval;

export default {
    name: 'tenantStatus',
    title: '在线租户',
    html: html,
    beforeMount: element => {
        console.info('马上要显示了');
    },
    mounted: (element, menudata) => {
        console.info('已经显示出来');

        var startTime = formatDateTime();

        interval = setInterval(function() {
            var table = $("table").DataTable();
            startTime = formatDateTime();
            table.draw();
        }, 3000);

        function formatDateTime() {
            var timestamp = Date.parse(new Date()) - 180000;
            var date = new Date(timestamp);
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            m = m < 10 ? ('0' + m) : m;
            var d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            var h = date.getHours();
            h = h < 10 ? ('0' + h) : h;
            var minute = date.getMinutes();
            var second = date.getSeconds();
            minute = minute < 10 ? ('0' + minute) : minute;
            second = second < 10 ? ('0' + second) : second;
            return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
        };

        element.find('table').dataTable({
            "sDom": 'rt<"clearfix table-bottom"ilp>',
            "searching": false,
            "ordering": false,
            "serverSide": true,
            "processing": false,
            "scrollX": false,
            "language": {
                'emptyTable': '当前暂无数据',
                'info': '第 _PAGE_ 页 / 总 _PAGES_ 页，',
                'infoEmpty': '',
                'lengthMenu': '每页 _MENU_ ',
                "paginate": {
                    "first": "第一页",
                    "last": "最后一页",
                    "next": "下一页",
                    "previous": "上一页"
                }
            },
            "pagingType": "full_numbers",
            "columns": [{
                    "className": 'text-c',
                    "data": "room"
                },
                {
                    "className": 'text-c',
                    "data": "tenantName"
                },
                {
                    "className": 'text-c',
                    "data": "channelName"
                },
                {
                    "className": 'text-c',
                    "data": "sn"
                }
            ],
            "ajax": function(data, callback, settings) {
                var pageSize = data.length;
                var page = data.start / pageSize + 1;
                data.pageSize = pageSize;
                data.page = page;
                data.startTime = startTime;
                data.groupId = $('#tree-panel').find(".selected").attr("data-id");
                $.myAjax({
                    url: "log/getCurrentTenants.do",
                    dataType: "json",
                    contentType: "application/json",
                    data: JSON.stringify(data),
                    success: function(result) {
                        result.recordsTotal = result.total;
                        result.recordsFiltered = result.total;
                        return callback(result);
                    }
                });
            }
        });
    },
    unmounted: element => {
        console.info('不显示了');
        clearInterval(interval);
    }
};
